package com.ruoyi.framework.interceptor;

import cn.hutool.core.date.DateUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * @author 王先亮
 * createTime: 2020/6/18.
 * description: 打印
 */
@Component
public class RequestPrintInterceptor implements HandlerInterceptor {

    /**
     * 解析时间
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        //打印请求内容
        StringBuilder sb = new StringBuilder("------------------------- 请求拦截 ")
                .append(DateUtil.now())
                .append(" --------------------------\n");
        sb.append("Url         : ").append(request.getMethod()).append(" ").append(request.getRequestURI()).append("\n");

        //请求的方法
        if (HandlerMethod.class.equals(handler.getClass())) {
            HandlerMethod method = (HandlerMethod) handler;
            Object controller = method.getBean();
            Class<?> controllerClass = controller.getClass();
            sb.append("Controller  : ").append(extractClassName(controllerClass.getName()))
                    .append(".(").append(extractClassName(controllerClass.getSimpleName())).append(".java:1)").append("\n");
            sb.append("Method      : ").append(method.getMethod().getName()).append("\n");
        }

        // 打印所有参数
        Enumeration<String> e = request.getParameterNames();
        int maxOutputLengthOfParaValue = 512;
        if (e.hasMoreElements()) {
            sb.append("Parameter   : ");
            while (e.hasMoreElements()) {
                String name = e.nextElement();
                String[] values = request.getParameterValues(name);
                if (values.length == 1) {
                    sb.append(name).append("=");
                    String value = values[0];
                    if (value != null && value.length() > maxOutputLengthOfParaValue) {
                        sb.append(value, 0, maxOutputLengthOfParaValue).append("...");
                    } else {
                        sb.append(value);
                    }
                } else {
                    sb.append(name).append("[]={");
                    for (int i = 0; i < values.length; i++) {
                        if (i > 0) {
                            sb.append(",");
                        }
                        sb.append(values[i]);
                    }
                    sb.append("}");
                }
                sb.append("  ");
            }
            sb.append("\n");
        }
        sb.append("--------------------------------------------------------------------------------\n");
        System.out.println(sb);
    }

    /**
     * 去除class编译的尾号,获取准确打印类名
     */
    private String extractClassName(String name) {
        int idx = name.indexOf("$");
        if (idx > 0) {
            return name.substring(0, idx);
        }
        return name;
    }
}
